package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;


    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //TODO
        IPage<ApArticle> page = new Page<>(0L, dto.getSize());
        Timestamp minTime = new Timestamp(dto.getMinBehotTime().getTime());
        Timestamp maxTime = new Timestamp(dto.getMaxBehotTime().getTime());
        if (type == 1) {
            //如果是加载更多
            articleMapper.selectPage(page, Wrappers.<ApArticle>lambdaQuery().eq(!"__all__".equals(dto.getTag()), ApArticle::getChannelId, dto.getTag())
                    .le(ApArticle::getPublishTime, minTime));
        } else if (type == 2) {
            //如果是加载更新
            articleMapper.selectPage(page, Wrappers.<ApArticle>lambdaQuery().eq(!"__all__".equals(dto.getTag()), ApArticle::getChannelId, dto.getTag())
                    .ge(ApArticle::getPublishTime, maxTime));
        } else {
            //如果是加载首页
            LambdaQueryWrapper<ApArticle> queryWrapper = Wrappers.<ApArticle>lambdaQuery().eq(!"__all__".equals(dto.getTag()), ApArticle::getChannelId, dto.getTag())
                    .between(ApArticle::getPublishTime, maxTime, minTime);
            this.page(page, queryWrapper);
        }

        //3.返回数据
        return ResponseResult.okResult(page.getRecords());
    }

    @Autowired
    private AmqpTemplate amqpTemplate;
    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //TODO
        //3.返回数据,要不要返回文章id ，因为wmnews表中需要
        if (ObjectUtil.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数失效");
        }

        ApArticle article = BeanUtil.toBean(dto, ApArticle.class);
        String title = dto.getTitle();
        LambdaQueryWrapper<ApArticle> queryWrapper = Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getTitle, title);
        ApArticle apArticle = this.getOne(queryWrapper);

        if (apArticle!=null) {
            LambdaQueryWrapper<ApArticle> queryWrapper1 = Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getId, apArticle.getId());
            this.update(article, queryWrapper1);
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,String.valueOf(article.getId()));
        }else {
            this.save(article);

            amqpTemplate.convertAndSend(
                    ArticleConstants.ARTICLE_ES_SYNC_QUEUE,
                    ArticleConstants.ARTICLE_ES_SYNC_ROUTINGKEY,
                    article.getId());
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(article.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            articleConfigMapper.insert(apArticleConfig);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.insert(apArticleContent);
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS, String.valueOf(article.getId()));
        }
    }



}
